﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.ServiceModel.DomainServices.Client.ApplicationServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AppLab.Data.Resources;
using AppLab.Web;
using AppLab.Web.Services;

namespace AppLab.Views
{
    public partial class ChangePassword : ChildWindow
    {
        private IList<OperationBase> possiblyPendingOperations = new List<OperationBase>();
        private FlowContext flow = new FlowContext();
        private ModifyPassword modifyPassword = new ModifyPassword();

        public ChangePassword()
        {
            InitializeComponent();
            this.DataContext = modifyPassword;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.CustomDataForm.ValidateItem())
            {
                //if (AdvanceValidateItem())
                //{
                //    modifyPassword.UserName = WebContext.Current.User.Name;
                //    this.modifyPassword.CurrentOperation = this.flow.UserChangedPassword(modifyPassword, this.ModifyPassword_Completed, null);
                //    this.possiblyPendingOperations.Add(this.modifyPassword.CurrentOperation);
                //    //防止用户迅速连接点击多次
                //    this.OKButton.IsEnabled = false;
                //}
            }
        }

        private void ModifyPassword_Completed(InvokeOperation<bool> operation)
        {
            if (!operation.IsCanceled)
            {
                if (operation.HasError)
                {
                    ErrorWindow.CreateNew(operation.Error);
                    operation.MarkErrorAsHandled();
                }
                else if (operation.Value)
                {
                    MessageBox.Show("密码修改成功！", "密码修改", MessageBoxButton.OK);
                    this.DialogResult = true;
                }
                else
                {
                    MessageBox.Show("密码修改失败！", "密码修改", MessageBoxButton.OK);
                }
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        /// <summary>
        /// Checks to ensure the password and confirmation match.  If they don't match,
        /// then a validation error is added.
        /// </summary>
        private bool AdvanceValidateItem()
        {
        //    // If either of the passwords has not yet been entered, then don't test for equality between
        //    // the fields.  The Required attribute will ensure a value has been entered for both fields.
        //    if (string.IsNullOrWhiteSpace(this.modifyPassword.NewPassword)
        //        || string.IsNullOrWhiteSpace(this.modifyPassword.ConfirmNewPassword))
        //    {
        //        return false;
        //    }
        //    // If the values are different, then add a validation error with both members specified
        //    if (this.modifyPassword.NewPassword != this.modifyPassword.ConfirmNewPassword)
        //    {
        //        this.modifyPassword.ValidationErrors.Add(new ValidationResult(
        //                ValidationErrorResources.ValidationErrorPasswordConfirmationMismatch, new string[] { "NewPassword", "ConfirmNewPassword" }));
        //        return false;
        //    }
            return true;
        }

        private void CustomDataForm_AutoGeneratingField(object sender, DataFormAutoGeneratingFieldEventArgs e)
        {
            // Put all the fields in adding mode
            e.Field.Mode = DataFieldMode.AddNew;

            if (e.PropertyName == "OldPassword")
            {
                PasswordBox passwordBox = (PasswordBox)e.Field.Content;
            }
            else if (e.PropertyName == "NewPassword")
            {
                PasswordBox passwordBox = (PasswordBox)e.Field.Content;
            }
            else if (e.PropertyName == "ConfirmNewPassword")
            {
                PasswordBox passwordBox = (PasswordBox)e.Field.Content;
            }
        }
    }
}

